package top.codingfly.netty.utils;

public class ByteExtUtil {

    public static byte[] longToBytes(long longValue) {
        byte[] result = new byte[8];
        for (int i = (result.length - 1); i >= 0; i--) {
            result[i] = (byte) (longValue & 0xFF);
            longValue >>= 8;
        }
        return result;
    }

    public static long bytesToLong(byte[] bytes) {
        long values = 0;
        for (int i = 0; i < 8; i++) {
            values <<= 8;
            values |= (bytes[i] & 0xff);
        }
        return values;
    }

    public static byte[] intToBytes(int intValue) {
        return new byte[]{
            (byte) ((intValue >> 24) & 0xFF),
            (byte) ((intValue >> 16) & 0xFF),
            (byte) ((intValue >> 8) & 0xFF),
            (byte) (intValue & 0xFF)
        };
    }

    public static int bytesToInt(byte[] bytes) {
        return bytes[3] & 0xFF |
            (bytes[2] & 0xFF) << 8 |
            (bytes[1] & 0xFF) << 16 |
            (bytes[0] & 0xFF) << 24;
    }

}
